#
# @lc app=leetcode.cn id=5 lang=python3
#
# [5] 最长回文子串
#


# @lc code=start
class Solution:
    def makePalindrome(self, s, i, j):
        if i < j < len(s) and s[i] != s[j]:
            return ''

        while i > 0 and j <= len(s) - 2:
            if s[i - 1] == s[j + 1]:
                i -= 1
                j += 1
            else:
                break
        return s[i:j + 1]

    def longestPalindrome(self, s: str) -> str:
        if not s:
            return ''

        maxStr = s[0]
        for i in range(len(s)):
            str1 = self.makePalindrome(s, i, i)
            str2 = self.makePalindrome(s, i, i + 1)
            str1 = str1 if len(str1) > len(str2) else str2
            maxStr = maxStr if len(maxStr) > len(str1) else str1

        return maxStr


# @lc code=end
